﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Imaginarium.team;
using Imaginarium.items.equip;
using Imaginarium.stats;
using System.Xml;
using Imaginarium.XML;

namespace Imaginarium.team
{
    public class CharacterEquipment : ICloneable
    {
        public Equipment[] equipments = new Equipment[8];
        Character character;

        public CharacterEquipment()
        {
        
        }
        public CharacterEquipment(XmlElement element)
        {
            XmlNodeList equipList = XmlHelper.getXmlNodeListByTagname(element, XmlParameters.CHARACTER_EQUIPMENT_LIST);
            if ((equipList == null || equipList.Count == 0)) return;
            int i = 0;
            foreach (XmlElement xe in equipList) {
                equipments[i] = new Equipment(xe);
                i++;
            }
        }

        public bool equip(Equipment eq, EquipmentPosition position)
        {
            if (!canEquip(eq, position)) return false;
            equipments[(int)EquipmentPosition.PrimaryHand] = eq;
            if (((Weapon)eq).twoHanded == true)
            {
                equipments[(int)EquipmentPosition.secondaryHand] = null;
            }
            return true;
        }

        public Stats compare(Equipment eq, EquipmentPosition position)
        {
            Equipment equiped = getEquipedItemByPosition(position);
            if (equiped == null)
                return eq.equipmentStats;
            Stats equipedStats = equiped.equipmentStats;
            Stats newItemStats = eq.equipmentStats;
            if (equipedStats.isPercentage)
            {
                equipedStats = character.stats * equipedStats;
            }
            if (newItemStats.isPercentage)
            {
                newItemStats = character.stats * newItemStats;
            }
            return eq.equipmentStats - equiped.equipmentStats;
        }

        public Equipment getEquipedItemByPosition(EquipmentPosition position)
        {
            return equipments[(int)position];
        }

        public Stats getFinalEquipmentStats() {
            Stats ans = new Stats();
            foreach (Equipment e in equipments) {
                ans += (e.equipmentStats.isPercentage == false) ? e.equipmentStats : (e.equipmentStats * character.stats);
            }
            return ans;
        }

        public bool canEquip(Equipment eq, EquipmentPosition position)
        {
            //Valida por job y lvl
            if (character is Player && !eq.jobsPermitted.Contains(((Player)character).currentJob.job))
                return false;
            if (eq.minBaseLvl > character.level)
                return false;

            //Valida por posible position
            if (!eq.positionPermitted.Contains(position))
                return false;

            //Valida por stats
            if (eq.minAgi > character.stats.agility)
                return false;
            if (eq.minDex > character.stats.dexterity)
                return false;
            if (eq.minInt > character.stats.intelligence)
                return false;
            if (eq.minStr > character.stats.strength)
                return false;

            //Valida por equipamiento Actual
            //if (position == EquipmentPosition.secondaryHand && primaryHand.twoHanded)
            //  return false;

            return true;
        }
        public object Clone()
        {
            Type type = this.GetType();
            DetailEquipment newDetailEquipment = (DetailEquipment)Activator.CreateInstance(type);
            throw new NotImplementedException();
        }
    }
}
